package aplicacao.campeonato;

import javax.swing.JOptionPane;
import modelo.Luta;
import modelo.excecoes.DAOException;
import modelo.excecoes.LutaComandoException;

/**
 *
 * @author  Renato Pinheiro
 */
public class DefineVencedor extends javax.swing.JDialog {

    /** Luta ativa */
    private Luta lutaAtiva;

            
    /** Creates new form DisplayWO */
    public DefineVencedor(java.awt.Dialog parent, boolean modal, 
            Luta lutaAtiva) {
        super(parent, modal);
        initComponents();
        
        this.lutaAtiva = lutaAtiva;
        
        // Não é possível definir vencedor com dois WOs.
        if(lutaAtiva.isWoLutador1() && lutaAtiva.isWoLutador2()){
            JOptionPane.showMessageDialog( this, "Não é possível definir um " +
                    "vencedor para uma luta com WO duplo");
            this.dispose();
        }
        
        if(!lutaAtiva.isPausada()){
            try {
                // Pausa a luta
                lutaAtiva.pausar();
            } catch (LutaComandoException ex) {
                JOptionPane.showMessageDialog( this, ex.getMessage(), "Erro", 
                        JOptionPane.ERROR_MESSAGE);            
            }
            
        }

        // Ajusta o nome do lutador 1
        if(lutaAtiva.getLutador1() != null)
            jchkLutador1.setText(lutaAtiva.getLutador1().getNome());
        else {
            jchkLutador1.setText("----------");
            jchkLutador1.setEnabled(false);
        }

        // Ajusta o nome do lutador 2
        if(lutaAtiva.getLutador2() != null)
            jchkLutador2.setText(lutaAtiva.getLutador2().getNome());
        else {
            jchkLutador2.setText("----------");
            jchkLutador2.setEnabled(false);
        }
        
        //Desabilitando lutador com WO
        if(lutaAtiva.isWoLutador1())
            jchkLutador1.setEnabled(false);

        if(lutaAtiva.isWoLutador2())
            jchkLutador2.setEnabled(false);            

        // Posicionando, ajustando o tamanho e exibindo a tela
        this.setLocationRelativeTo(null);
        this.setSize(380, 290); // 363, 270
        this.setVisible(true);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        jchkLutador1 = new javax.swing.JCheckBox();
        jchkLutador2 = new javax.swing.JCheckBox();
        jlTitulo = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jbConfirmar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaObservacoes = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("");

        buttonGroup.add(jchkLutador1);
        jchkLutador1.setFont(new java.awt.Font("Arial", 1, 12));
        jchkLutador1.setText("Lutador 1");

        buttonGroup.add(jchkLutador2);
        jchkLutador2.setFont(new java.awt.Font("Arial", 1, 12));
        jchkLutador2.setText("Lutador 2");

        jlTitulo.setFont(new java.awt.Font("Arial", 1, 12));
        jlTitulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlTitulo.setText("Selecione o lutador vencedor");

        jbConfirmar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aplicacao/imagens/icones/accept.png"))); // NOI18N
        jbConfirmar.setText("Confirmar");
        jbConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConfirmarActionPerformed(evt);
            }
        });

        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aplicacao/imagens/icones/cancel.png"))); // NOI18N
        jbCancelar.setText("Cancelar");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Observações", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jtaObservacoes.setColumns(20);
        jtaObservacoes.setRows(5);
        jScrollPane1.setViewportView(jtaObservacoes);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jchkLutador2)
                    .addComponent(jchkLutador1)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                    .addComponent(jlTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbConfirmar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 141, Short.MAX_VALUE)
                        .addComponent(jbCancelar))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbCancelar, jbConfirmar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jchkLutador1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jchkLutador2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbConfirmar)
                    .addComponent(jbCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConfirmarActionPerformed
        
    try {

        // De acordo com o lutador selecionado seta o vencedor da luta
        if (jchkLutador1.isSelected()) {
            lutaAtiva.setLutadorVencedor(lutaAtiva.getLutador1());
        }
        
        if (jchkLutador2.isSelected()) {
            lutaAtiva.setLutadorVencedor(lutaAtiva.getLutador2());
        }
        
        lutaAtiva.setObservacoes(jtaObservacoes.getText());

        lutaAtiva.encerrar();

    } catch (DAOException ex) {
        JOptionPane.showMessageDialog( this, ex.getMessage(), "Erro", 
                JOptionPane.ERROR_MESSAGE);
        throw new RuntimeException("Erro no acesso ao banco de dados");
    } catch (LutaComandoException ex) {
        JOptionPane.showMessageDialog( this, ex.getMessage(), "Erro", 
                JOptionPane.ERROR_MESSAGE);
    }
            
    this.dispose();
    
}//GEN-LAST:event_jbConfirmarActionPerformed

private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
    this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbConfirmar;
    private javax.swing.JCheckBox jchkLutador1;
    private javax.swing.JCheckBox jchkLutador2;
    private javax.swing.JLabel jlTitulo;
    private javax.swing.JTextArea jtaObservacoes;
    // End of variables declaration//GEN-END:variables

}
